<?php 
	if(!isset($_SESSION['tkemail'])){
		if(!isset($_COOKIE['role'])){
			//$_SESSION['action']='entry';
			header( 'Location: index.php?action=entry' );
		}else{
			$_SESSION['tkemail']=$_COOKIE["tkemail"];
		    $_SESSION['role']=$_COOKIE["role"];
			
			$role=$_SESSION['role'];
			$tkemail=$_SESSION['tkemail'];
		}
	}else{
		$role=$_SESSION['role'];
		$tkemail=$_SESSION['tkemail'];
	}
	if($tkemail==""){
		header( 'Location: index.php?action=entry' );
	}

?>


<link rel="stylesheet" type="text/css" href="js/jsDatePick_ltr.min.css" />
<link rel="stylesheet" type="text/css" href="css/style-login.css" />
<script type="text/javascript" src="js/jsDatePick.min.1.3.js"></script>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.1/jquery.min.js"></script>
<script type="text/javascript">
<!--
function paging(page)
{
	if (window.XMLHttpRequest){// code for IE7+, Firefox, Chrome, Opera, Safari
		xmlhttp=new XMLHttpRequest();
	}else{// code for IE6, IE5
		xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
	}
	xmlhttp.onreadystatechange=function(){
										  if (xmlhttp.readyState==4 && xmlhttp.status==200){
										    	document.getElementById("giayphep1").innerHTML=xmlhttp.responseText;
										  }
									  }
	xmlhttp.open("GET","ajax/pagingLicense.php?page="+page,true);
	xmlhttp.send();
}
function calleditLicense(email,date_start,date_stop,license_count,max_server,page,ele){
	popuploadbel(ele);
	document.getElementById("email").value=email;
	document.getElementById("date_start").value=date_start;
	document.getElementById("date_stop").value=date_stop;
	document.getElementById("license_count").value=license_count;
	document.getElementById("max_server").value=max_server;
	document.getElementById("page").value=page;
}

function editLicense(){
	
	var email=document.getElementById("email").value;
	var date_start=document.getElementById("date_start").value;
	var date_stop=document.getElementById("date_stop").value;
	var license_count=document.getElementById("license_count").value;
	var max_server=document.getElementById("max_server").value;
	var page=document.getElementById("page").value;
	
	
	if (window.XMLHttpRequest){// code for IE7+, Firefox, Chrome, Opera, Safari
		xmlhttp=new XMLHttpRequest();
	}else{// code for IE6, IE5
		xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
	}
	xmlhttp.onreadystatechange=function(){
		  if (xmlhttp.readyState==4 && xmlhttp.status==200){
		    	document.getElementById("giayphep1").innerHTML=xmlhttp.responseText;
		  }
	  }
	xmlhttp.open("GET","ajax/editLicense.php?action=editLicense&email="+email+"&date_start="+date_start+"&date_stop="+date_stop+"&license_count="+license_count+"&max_server="+max_server+"&page="+page,true);
	xmlhttp.send();
}
//-->
</script>
<style>
* {font-family:Arial, Helvetica, sans-serif; font-size:14px}
#fade { /*--Transparent background layer--*/
        display: none; /*--hidden by default--*/
        background: #727272;
        position: fixed; left: 0; top: 0;
        width: 100%; height: 100%;
        opacity: .50;
        z-index: 9999;
}
.popup_block{
        display: none; /*--hidden by default--*/
        background:url(images/bgn.png);
       
		border-radius: 8px;
        float: left;
        font-size: 1.2em;
        position: fixed;
        top: 50%; left: 50%;
        z-index: 99999;
		width:463px;
		height:400px;
		
        /*--CSS3 Box Shadows--*/
        /*--CSS3 Rounded Corners--*/
}
.btn_close {
	background:url(images/close.png);
        float: right;
        margin: -10px -10px 0 0;
		width:30px;
		height:30px;
}
/*--Making IE6 Understand Fixed Positioning--*/
*html #fade {
        position: absolute;
}
*html .popup_block {
        position: absolute;
}

.btnupbg {
	/*background: none repeat scroll 0 0 #79AA38;*/
	background:url(images/btn.png) no-repeat;
    border-color: #C4C0B9;
    border-radius: 5px 5px 5px 5px;
    border-width: 0;
    color: white;
    cursor: pointer;
    font-weight: bold;
    height: 28px;
    margin: 6px 0 0 20px;
    padding: 0 5px;
    width: 65px;}
.btnupbg:hover {background:#309DCF}
.btnupload {background: -moz-linear-gradient(center top , #63B6DB, #309DCF) repeat;}
.text{ height:24px;}
</style>
<?php 
	$lang=$_SESSION['lang'];
	if($lang=='')
			$lang='EN';
	
	if ( !isset($_REQUEST['page'] ))
	{
    	$page = 1 ;
	}else{
		$page = $_REQUEST['page'] ;
	}
	
	$rows_per_page = 20;
	$page_start = ( $page - 1 ) * $rows_per_page; 
	$page_end = $page * $rows_per_page;
	
?>
	<div id="popup_name" class="popup_block" style="height:270px">
			<input type="hidden" style="width: 190px" id="page" />
           	<table style="width:100%;height:90%">
            		<tr >
            			<td style="width: 40%"><span style="color: #027acc;padding-left:30px;"><?=$address_email?> : </span></td>
            			<td>
            				<input type="text" style="width: 190px" id="email"  readonly="readonly"/>
            			</td>
            		</tr>
            		<tr>
            			<td><span style="color: #027acc;padding-left:30px;"><?=$date_start?> : </span></td>
            			<td>
            				<input type="text" style="width: 190px" id="date_start" readonly="readonly" />
            			</td>
            		</tr>
            		<tr>
            			<td><span style="color: #027acc;padding-left:30px;"><?=$date_stop?> : </span></td>
            			<td>
            				<input type="text" style="width: 190px" id="date_stop" readonly="readonly" />
            			</td>
            		</tr>
            		<tr>
            			<td><span style="color: #027acc;padding-left:30px;"><?=$license_count?> : </span></td>
            			<td>
            				<input type="text" style="width: 190px" id="license_count" readonly="readonly" />
            			</td>
            		</tr>
            		<tr>
            			<td><span style="color: #027acc;padding-left:30px;">Max server : </span></td>
            			<td>
            				<input type="text" style="width: 190px" id="max_server" />
            			</td>
            		</tr>
            		<tr>
            			<td></td>
            			<td style="align:center">
				   			<input type="button" onclick="editLicense()" style="font-family:Arial, Helvetica, sans-serif; font-size:12px;" class="btnupbg iclose" value="<?=$edit?>" name="changePass" />
				            <input type="submit" class="btnupbg iclose"  style="font-family:Arial, Helvetica, sans-serif; font-size:12px;" value="<?=$cancel?>"  name="cancel" />
            			</td>
            		</tr>
            	</table>
    </div>

    <div class="maincon1">
        <div class="hbox">
           <h2><?=$login?> </h2>
            <p><?=$login1?></p>
            <div class="seldown">
            	<ul>
			<?php if ($role==0){?>
				<li class="hoso" onclick="changePage('profile')" style="float:left;">
		        	<a href="#" style="background:url(images/bg_aa_normal.png) no-repeat; display:block; width:145px; height:71px"><span><?=$hoso?></span></a>
		        </li>
		        <li class="giayphep" style="float:left;">
		        	<a onclick="changePage('license')" style="background:url(images/bg_aa.png) no-repeat; display:block; width:145px; height:71px;cursor: pointer;"><span class="chooselicense"><?=$namelicense?></span></a>
		        </li>
			<?php }else if ($role==1){?>
				<li class="hoso" onclick="changePage('profile')" style="float:left;">
		        	<a href="#" style="background:url(images/bg_aa_normal.png) no-repeat; display:block; width:145px; height:71px"><span><?=$hoso?></span></a>
		        </li>
		        <li class="giayphep" style="float:left;">
		        	<a onclick="changePage('license')" style="background:url(images/bg_aa.png) no-repeat; display:block; width:145px; height:71px;cursor: pointer;"><span class="chooselicense"><?=$namelicense?></span></a>
		        </li>
		        <li class="server" style="float:left;">
		        	<a href="#"  onclick="changePage('log_server')" style="background:url(images/bg_aa_normal.png) no-repeat; display:block; width:145px; height:71px"><span><?=$logserver?></span></a>
		        </li>
		        <li class="viewer" style="float:left;">
		        	<a href="#"  onclick="changePage('logviewer')" style="background:url(images/bg_aa_normal.png) no-repeat; display:block; width:145px; height:71px"><span><?=$logviewer?></span></a>
		        </li>
		        <li class="viewertoserver" style="float:left;">
		        	<a href="#"  onclick="changePage('log_viewertoserver')" style="background:url(images/bg_aa_normal.png) no-repeat; display:block; width:145px; height:71px"><span><?=$logviewertoserver?></span></a>
		        </li>
			<?php }?>
		    	
	  	  </ul>
            </div>
        </div>
    </div>
    <div class="maind"> 
    	<?php if($role==0){?>
    	<div id="giayphep2" style="padding-left: 40px;"> 
    	<?php 
				include 'connection/connection.php';
				
				
				
    			$query=mysql_query("SELECT *
									FROM log_buy b
									WHERE b.email='$tkemail';");
    			
				 $list_license=array();
				 $email_temp="";
				 $co=0;
				 
			?>
	       <table width="900" cellpadding="0" cellspacing="0" class="mtable">
	    	<thead>
	        	<tr>
	                <td align="center"><?=$address_email?></td>
	                <td align="center"><?=$buy_date?></td>
	                <td align="center"><?=$type_license?></td>
	                <td align="center"><?=$date_start?></td>
	                <td align="center"><?=$date_stop?></td>
	                 <td align="center"><?=$license_count?></td>
	            </tr>
	        </thead>
	       <?php 
	        while ($row=mysql_fetch_array($query)){
	        			
	        			if($row['buy_type']==1){
	        				$buy_type=$free;
	        				$color='#E2EFF8';
	        			}else {
	        				$buy_type=$money;
	        				$color='#F7EAE8';
	        			}
	        			$color="white";
	        		?>
		        <tr >
	        		<td><?=$row['email']?></td>
	        		<td align="center"><?=$row['buy_date']?></td>
	        		<td align="center"><?=$buy_type?></td>
            		<td align="center"><?=$row['date_start']?></td>
            		<td align="center"><?=$row['date_stop']?></td>
            		<td align="center"><?=$row['license_count']?></td>
		        </tr>
		      <?php 
	        } 
	        
			mysql_close($connection);	 
    	
	        	?>
	    </table>  
	    </div>
	    <?php } else {
    				?>
    	<div id="giayphep1" style="padding-left: 40px;"> 
    	<?php 
				include 'connection/connection.php';
    			$query=mysql_query("SELECT * FROM log_buy b,user_info u WHERE b.email=u.email AND u.role=0;");
    			
    			$number_of_page = ceil ( mysql_num_rows( $query ) / $rows_per_page ); 
				 $list_license=array();
				 $email_temp="";
				 $co=0;
			?>
	       <table width="900" cellpadding="0" cellspacing="0" class="mtable">
	    	<thead>
	        	<tr>
	        		<td align="left" style="width: 3%"><?php echo $no?></td>
	                <td align="center"><?=$address_email?></td>
	                <td align="center"><?=$buy_date?></td>
	                <td align="center"><?=$type_license?></td>
	                <td align="center"><?=$date_start?></td>
	                <td align="center"><?=$date_stop?></td>
	                <td align="center"><?=$license_count?></td>
	                <td align="center">max server</td>
	            </tr>
	        </thead>
	       <?php 
	       $i=1;
	        while ($row=mysql_fetch_array($query)){
	        			
	        			if($row['buy_type']==1){
	        				$buy_type=$free;
	        				$color='#E2EFF8';
	        			}else {
	        				$buy_type=$money;
	        				$color='#F7EAE8';
	        			}
	        			$color="white";
	        			if ( $i > $page_start ){
	        		?>
		        <tr >
		        	<td align="left"  style="width: 3%"><?=$i?></td>
	        		<td><a href="#?w=400" style="color: blue" onclick="calleditLicense('<?=$row['email']?>','<?=$row['date_start']?>','<?=$row['date_stop']?>','<?=$row['license_count']?>','<?=$row['max_server']?>','<?=$page?>',this)" rel="popup_name" class="poplight"><?=$row['email']?></a></td>
	        		<td align="center"><?=$row['buy_date']?></td>
	        		<td align="center"><?=$buy_type?></td>
            		<td align="center"><?=$row['date_start']?></td>
            		<td align="center"><?=$row['date_stop']?></td>
            		<td align="center"><?=$row['license_count']?></td>
            		<td align="center"><?=$row['max_server']?></td>
		        </tr>
		      <?php 
	        			
					
						if ($i >= $page_end){
							break;
						}
	        			}
	        			$i++;
	        } 
	        
			mysql_close($connection);	 
		
	        	?>
	        	<tr>
						<td colspan="9" class="rf-dt-shdr-c">
							<span style="padding:1px; display: inline-block;">
														
								<?php if ( $number_of_page > 1 ) 
								{ 
									if ($page==1)
										$p=2;
									else if ($page==$number_of_page)
										$p=$number_of_page-1;
									else $p=$page;
									if($page!=1){?>
										<a onclick="paging(<?=1?>)" ><span class='button_next'><?php echo "<<<";?></span> </a>
										<a onclick="paging(<?=($page-1)?>)"><span class='button_next'><?php echo "<<";?></span> </a>
									<?php  }
									else{
										echo "<span class='button_next'><<<</span> "; 
										echo "<span class='button_next'><<</span> ";
									}
									for ( $i = $p-1; $i <= $p+1; $i++ ) {	
											if ($i<=$number_of_page && $i>0)
											
												if ($i==$page){
													echo "<span class='button_next'><b>".$i."</b></span>"; 
												}else{
												?>
													<a onclick="paging(<?=$i?>)"><span class="button_next">
													<?php 
													echo $i;
													?>
													</span> </a>
													<?php 
												}
											?>
										
									<?php 
											}
										
										if($page!=$number_of_page){ ?>
											<a onclick="paging(<?=($page+1)?>)"><span class='button_next'><?php echo ">>";?></span> </a>
											<a onclick="paging(<?=($number_of_page)?>)"><span class='button_next'><?php echo ">>>";?></span> </a>
										<?php }
										else{
											echo "<span class='button_next'>>></span> ";
											echo "<span class='button_next'>>>></span> ";
										}
									}
								
									
									?>
																						
							</span>
						</td>
					</tr>
	    </table>  
	    </div>
	    <?php 
	    }
	    ?>
    </div>